package com.mr.cai.main.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cai.mr.lib.utils.LogInfo;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.MrCaiFavouriteActivity;
import com.mr.cai.main.activity.MrCaiFeedbacksActivity;
import com.mr.cai.main.activity.MrCaiOrdersActivity;
import com.mr.cai.main.activity.MrCaiRecommendActivity;
import com.mr.cai.main.activity.MrCaiRemainActivity;
import com.mr.cai.main.activity.MrCaiScoresActivity;
import com.mr.cai.main.activity.MrCaiSettingActivity;
import com.mr.cai.main.activity.MrCaiTicketsActivity;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.view.RoundImageView;

import rx.functions.Action1;

/**
 * Created by hai8108 on 16/2/10.
 */
public class MyPageFragment extends BaseFragment {
    private final int MR_CAI_USER_INFO_NUM = 6;
    private RoundImageView mrCaiUserIv;
    private TextView mrCaiUserPhoneTv;
    private ImageView mrCaiSetIv;
    private View mrCaiUserScoreView, mrCaiUserTicketView, mrCaiUserRemainView;
    private TextView mrCaiUserScoreTv, mrCaiUserTicketTv, mrCaiUserRemainTv;
    private View[] mrCaiUserInfoViews = new View[MR_CAI_USER_INFO_NUM];
    private ImageView[] mrCaiIconViews = new ImageView[MR_CAI_USER_INFO_NUM];
    private TextView[] mrCaiTextViews = new TextView[MR_CAI_USER_INFO_NUM];
    private int[] iconResId = {R.drawable.mr_cai_orders_bg, R.drawable.mr_cai_favourite_bg, R.drawable
            .mr_cai_feedback_bg, R.drawable.mr_cai_recommend_bg, R.drawable.mr_cai_cooking_page_bg,
            R.drawable.mr_cai_sys_msg_bg};
    private int[] txtResId = {R.string.my_sum_orders_txt, R.string.my_sum_favourite_txt, R.string
            .my_sum_feedback_txt, R.string.my_sum_recommend_txt,
            R.string.my_sum_cookbooks_txt, R.string.my_sum_sysmsg_txt};

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mPublicLayout = CommonCoreUtil.createPage(getActivity(), R.layout
                .fragment_page_my_layout);
        return mPublicLayout;
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findView();
        initData();
    }

    private void findView () {
        mrCaiSetIv = (ImageView) mPublicLayout.findViewById(R.id.my_setting_iv);
        CommonCoreUtil.setClickAction(mrCaiSetIv, new Action1() {
            @Override
            public void call (Object o) {
                MrCaiSettingActivity.launch(getActivity());
            }
        });

        mrCaiUserScoreView = mPublicLayout.findViewById(R.id.mr_cai_sum_score_layout);
        mrCaiUserScoreTv = (TextView) mrCaiUserScoreView.findViewById(R.id.my_sum_score);
        CommonCoreUtil.setClickAction(mrCaiUserScoreView, new Action1() {
            @Override
            public void call (Object o) {
                MrCaiScoresActivity.launch(getActivity());
            }
        });
        mrCaiUserTicketView = mPublicLayout.findViewById(R.id.mr_cai_sum_ticket_layout);
        mrCaiUserTicketTv = (TextView) mrCaiUserTicketView.findViewById(R.id.my_sum_ticket);
        CommonCoreUtil.setClickAction(mrCaiUserTicketView, new Action1() {
            @Override
            public void call (Object o) {
                MrCaiTicketsActivity.launch(getActivity());
            }
        });

        mrCaiUserRemainView = mPublicLayout.findViewById(R.id.mr_cai_sum_remain_layout);
        mrCaiUserRemainTv= (TextView) mrCaiUserRemainView.findViewById(R.id.my_sum_remaining);
        CommonCoreUtil.setClickAction(mrCaiUserRemainView, new Action1() {
            @Override
            public void call (Object o) {
                MrCaiRemainActivity.launch(getActivity());
            }
        });
        mrCaiUserIv = (RoundImageView) mPublicLayout.findViewById(R.id.user_icon);
        mrCaiUserPhoneTv = (TextView) mPublicLayout.findViewById(R.id.user_phone_num);

        mrCaiUserInfoViews[0] = mPublicLayout.findViewById(R.id.my_orders);
        mrCaiUserInfoViews[1] = mPublicLayout.findViewById(R.id.my_favourites);
        mrCaiUserInfoViews[2] = mPublicLayout.findViewById(R.id.my_feedbacks);
        mrCaiUserInfoViews[3] = mPublicLayout.findViewById(R.id.my_recommends);
        mrCaiUserInfoViews[4] = mPublicLayout.findViewById(R.id.my_cookbooks);
        mrCaiUserInfoViews[5] = mPublicLayout.findViewById(R.id.my_messages);

        for (int i = 0; i < MR_CAI_USER_INFO_NUM; i++) {
            mrCaiUserInfoViews[i].setOnClickListener(onClick);
            mrCaiIconViews[i] = (ImageView) mrCaiUserInfoViews[i].findViewById(R.id.mr_cai_set_icon);
            mrCaiIconViews[i].setBackgroundResource(iconResId[i]);
            mrCaiTextViews[i] = (TextView) mrCaiUserInfoViews[i].findViewById(R.id.mr_cai_set_txt);
            mrCaiTextViews[i].setText(txtResId[i]);
        }
    }
    public final static int FROM_RECOMMEND_ITEM = 0x100;
    public final static int FROM_COOKPAGES_ITEM = 0x101;
    private View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            if(view == mrCaiUserInfoViews[0]){
                LogInfo.log("haitian", "viewId="+view.getId());
                MrCaiOrdersActivity.launch(getActivity());
            } else if(view == mrCaiUserInfoViews[1]){
                MrCaiFavouriteActivity.launch(getActivity());
            }else if(view == mrCaiUserInfoViews[2]){
                MrCaiFeedbacksActivity.launch(getActivity());
            }else if(view == mrCaiUserInfoViews[3]){
                MrCaiRecommendActivity.launch(getActivity(), FROM_RECOMMEND_ITEM);
            }else if(view == mrCaiUserInfoViews[4]){
                MrCaiRecommendActivity.launch(getActivity(), FROM_COOKPAGES_ITEM);
            }else if(view == mrCaiUserInfoViews[5]){
            }
        }
    };
    private void initData () {

    }
}
